let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    search: {
      page: 1,
      rows: 10,
      type: "",
      order: ""
    },
    option1: [{
        text: '全部商品',
        value: ''
      },
      {
        text: '新款商品',
        value: '1'
      },
      {
        text: '活动商品',
        value: '2'
      },
    ],
    option2: [{
        text: '默认排序',
        value: ''
      },
      {
        text: '最低价格',
        value: '1'
      },
      {
        text: '最高价格',
        value: '2'
      },
      {
        text: '销量排序',
        value: '3'
      },
    ],
    value1: '',
    value2: '',
    list: {},
    typeid: "",
    page: 1,
  },
  addCar() {
    console.log('加入购物车')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;

    if (options.typeid) {
      this.setData({
        typeid: options.typeid
      })
    }

    _this.getdata();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 获取商品列表
   */
  getdata: function () {
    let _this = this;
    this.data.search.type = this.data.value1;
    this.data.search.order = this.data.value2;
    this.data.search.typeid = this.data.typeid;
    this.data.search.page = this.data.page,
    wx.showLoading({
      title: '加载中',
    })

    
      App._post_form('wxapi/goods/lists', this.data.search, function (result) {


        var contentlistTem = _this.data.list;
        if (_this.data.page == 1) {
          contentlistTem = []
        }
        var contentlist = result.data;
        if (result.data.length == 0) {
          wx.showToast({
            title: '没有更多数据',
          })
          _this.setData({
            best: contentlistTem,
            hasMoreData: false
          })
        } else {

          console.log("附加了", contentlistTem, contentlist)
          _this.setData({
            list: contentlistTem.concat(contentlist),
            hasMoreData: true,
            page: _this.data.page + 1
          })
        }

      });
  },
  addCar(e) {
    let id = e.currentTarget.dataset.id;
    console.log(e)
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + id,
    });
  },
  onBeforeChange1(i) {
    console.log("e1", i.detail);
    this.setData({
      value1: i.detail,
    })

    this.getGoodsList();
  },
  onBeforeChange2(i) {
    console.log("e2", i.detail);
    this.setData({
      value2: i.detail
    })

    this.getGoodsList();
  },
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.getdata()
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },
})